# coding:utf-8
from flask import Flask
from flask import jsonify
from flask import request
from flask import send_file
from sys_utils import create_logger, create_ssh_client, LOG

import api_handler

app = Flask(__name__)

handler_manager = None

def init_service():
    global handler_manager
    handler_manager = api_handler.init_handler_manager()
    api_handler.register_handlers(handler_manager)
    api_handler.fetch_ports_info(handler_manager)
    api_handler.fetch_flows_info(handler_manager)
    #api_handler.init_traffic_collector(handler_manager)

# REST Service Entrance
def run_service():
    create_logger()
    create_ssh_client()
    init_service()
    app.run(host='0.0.0.0')

'''
Static File API MainService
'''
@app.route('/')
def index():
    LOG("send_file: ../static/index.html")
    return send_file('../static/index.html')

@app.route('/<filename>')
def index_html(filename):
    target_filename = '../static/' + filename
    LOG("send_file: " + target_filename)
    return send_file(target_filename)

@app.route('/<path>/<filename>')
def index_resource(path, filename):
    target_filename = '../static/' + path + '/' + filename
    LOG("send_file: " + target_filename)
    return send_file(target_filename)

'''
REST API MainService
--> Basic OVS Operation
'''
@app.route('/rest/get_phyports_status', methods=['GET'])
def get_phyports_status():
    params = {}
    params['action_type'] = 1
    LOG("get_phyports_status: " + str(params))
    ret = handler_manager.handle_cmd_event('base', params)
    return jsonify(ret)

@app.route('/rest/set_phyport_status', methods=['POST'])
def set_phyport_status():
    params = request.json
    params['action_type'] = 2
    LOG("set_phyport_status: " + str(params))
    ret = handler_manager.handle_cmd_event('base', params)
    return jsonify(ret)

@app.route('/rest/get_lagports_status', methods=['GET'])
def get_lagports_status():
    params = {}
    params['action_type'] = 3
    LOG("get_lagports_status: " + str(params))
    ret = handler_manager.handle_cmd_event('base', params)
    return jsonify(ret)

@app.route('/rest/set_lagport_status', methods=['POST'])
def set_lagport_status():
    params = request.json
    params['action_type'] = 4
    LOG("set_lagport_status: " + str(params))
    ret = handler_manager.handle_cmd_event('base', params)
    return jsonify(ret)

@app.route('/rest/get_logicgroup_status', methods=['GET'])
def get_logicgroup_status():
    params = {}
    params['action_type'] = 5
    LOG("get_logicgroup_status: " + str(params))
    ret = handler_manager.handle_cmd_event('base', params)
    return jsonify(ret)

@app.route('/rest/set_logicgroup_status', methods=['POST'])
def set_logicgroup_status():
    params = request.json
    params['action_type'] = 6
    LOG("set_logicgroup_status: " + str(params))
    ret = handler_manager.handle_cmd_event('base', params)
    return jsonify(ret)

@app.route('/rest/set_controller', methods=['POST'])
def set_controller():
    params = request.json
    params['action_type'] = 7
    LOG("set_controller: " + str(params))
    ret = handler_manager.handle_cmd_event('base', params)
    return jsonify(ret)

'''
REST API MainService
--> Basic Flow Operation
'''
@app.route('/rest/get_port_traffic_stat', methods=['POST'])
def get_port_traffic_stat():
    params = request.json
    params['action_type'] = 1
    LOG("get_port_traffic_stat: " + str(params))
    ret = handler_manager.handle_cmd_event('flow', params)
    return jsonify(ret)

@app.route('/rest/get_flow_traffic_stat', methods=['POST'])
def get_flow_traffic_stat():
    params = request.json
    params['action_type'] = 2
    LOG("get_flow_traffic_stat: " + str(params))
    ret = handler_manager.handle_cmd_event('flow', params)
    return jsonify(ret)

@app.route('/rest/get_flow_entries', methods=['GET'])
def get_flow_entries():
    params = {}
    params['action_type'] = 3
    LOG("get_flow_entries: " + str(params))
    ret = handler_manager.handle_cmd_event('flow', params)
    return jsonify(ret)

@app.route('/rest/add_flow_entry', methods=['POST'])
def add_flow_entry():
    params = request.json
    params['action_type'] = 4
    LOG("add_flow_entry: " + str(params))
    ret = handler_manager.handle_cmd_event('flow', params)
    return jsonify(ret)

@app.route('/rest/mod_flow_entry', methods=['POST'])
def mod_flow_entry():
    params = request.json
    params['action_type'] = 5
    LOG("mod_flow_entry: " + str(params))
    ret = handler_manager.handle_cmd_event('flow', params)
    return jsonify(ret)

@app.route('/rest/del_flow_entry', methods=['POST'])
def del_flow_entry():
    params = request.json
    params['action_type'] = 6
    LOG("del_flow_entry: " + str(params))
    ret = handler_manager.handle_cmd_event('flow', params)
    return jsonify(ret)

@app.route('/rest/get_group_tables', methods=['GET'])
def get_group_tables():
    params = {}
    params['action_type'] = 7
    LOG("get_group_tables: " + str(params))
    ret = handler_manager.handle_cmd_event('flow', params)
    return jsonify(ret)

@app.route('/rest/add_group_table', methods=['POST'])
def add_group_table():
    params = request.json
    params['action_type'] = 8
    LOG("add_group_table: " + str(params))
    ret = handler_manager.handle_cmd_event('flow', params)
    return jsonify(ret)

@app.route('/rest/mod_group_table', methods=['POST'])
def mod_group_table():
    params = request.json
    params['action_type'] = 9
    LOG("mod_group_table: " + str(params))
    ret = handler_manager.handle_cmd_event('flow', params)
    return jsonify(ret)

@app.route('/rest/del_group_table', methods=['POST'])
def del_group_table():
    params = request.json
    params['action_type'] = 10
    LOG("del_group_table: " + str(params))
    ret = handler_manager.handle_cmd_event('flow', params)
    return jsonify(ret)

'''
REST API TestCase
'''
@app.route('/test/path-param', methods=['GET'])
def test_path_param():
    '''
    ?后面是参数，可以通过request.args.get访问
    例如：/test/path-param?text=world
    '''
    print '------------HEADER--------------'
    print request.headers
    text = request.args.get('text')
    return '/test/path-param TEXT = ' + text

@app.route('/test/in-param/<input>', methods=['GET'])
def test_in_param(input):
    '''
    <input>对应路径变量信息
    例如：/test/in-param/hello
    '''
    print '------------HEADER--------------'
    print request.headers
    return '/test/in-param/INPUT = ' + input

@app.route('/test/body-param', methods=['POST'])
def test_body_param():
    '''
    <body>参数信息
    '''
    print '------------HEADER--------------'
    print request.headers
    print '-----------JSONBODY-------------'
    print request.json
    return '/test/body-param'

@app.route('/test/json-return', methods=['GET', 'POST'])
def test_json_return():
    '''
    测试返回JSON格式的数据
    '''
    ret = {}
    ret['key'] = 'kb'
    ret['val'] = 'st'
    return jsonify(ret)